package com.hp.gxw.u8.xml;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hp.gxw.u8.dto.RdRecord01Parameter;
import com.hp.gxw.u8.dto.RdRecords01Parameter;
import com.hp.gxw.utils.DateUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;


public class RdRecord01Xml {
	
	public static String createXML(RdRecord01Parameter rdRecord01Parameter) throws Exception{
		try {
			// 1、创建document对象
			Document document = DocumentHelper.createDocument();
			// 2、创建根节点rss
			Element ufinterface = document.addElement("ufinterface");
			ufinterface.addNamespace("z", "http://www.w3.org/2001/XMLSchema");
			// 向ufinterface节点添加efserverid属性(U8账套)
			ufinterface.addAttribute("efserverid", rdRecord01Parameter.getEfserverid());
			// 向ufinterface节点添加eftype属性(方法：24代表采购入库)
			ufinterface.addAttribute("eftype", rdRecord01Parameter.getEftype());
			// 向ufinterface节点添加proc属性(增删改查事件)
			ufinterface.addAttribute("proc", rdRecord01Parameter.getProc());
			// 向ufinterface节点添加efdebug属性
			ufinterface.addAttribute("efdebug", "1");
			// 向ufinterface节点添加actions属性,入库后自动审核
			ufinterface.addAttribute("actions", "verify");
			ufinterface.addAttribute("ddate", DateUtil.getDateFormat(rdRecord01Parameter.getDdate()));
			// 4、生成子节点及子节点内容
			Element voucher = ufinterface.addElement("voucher");
			voucher.addAttribute("cardnumber", rdRecord01Parameter.getEftype());
			voucher.addAttribute("ccardname", "");
			voucher.addNamespace("z", "EF");
			Element head = voucher.addElement("head");
			Element headRow = head.addElement("z:row");
			headRow.addAttribute("cvouchtype", rdRecord01Parameter.getCvouchtype());// 传入参数：单据类型
			headRow.addAttribute("cbustype", rdRecord01Parameter.getCbustype());// 传入参数
			headRow.addAttribute("cptcode", rdRecord01Parameter.getCptcode());// 传入参数
			headRow.addAttribute("csource", rdRecord01Parameter.getCsource());// 传入参数
			headRow.addAttribute("cwhcode", rdRecord01Parameter.getCwhcode());// 传入参数：仓库
			headRow.addAttribute("ccode", rdRecord01Parameter.getCcode());// 传入参数：到货单号
			headRow.addAttribute("cvencode", rdRecord01Parameter.getCvencode());// 传入参数：供应商
			if(rdRecord01Parameter.getCdepcode() != null){
				headRow.addAttribute("cdepcode", rdRecord01Parameter.getCdepcode());// 传入参数：供应商
			}
			if(rdRecord01Parameter.getCordercode() != null){
				headRow.addAttribute("cordercode", rdRecord01Parameter.getCordercode());// 传入参数：采购订单号
			}
			if(rdRecord01Parameter.getCbarvcode() != null){
				headRow.addAttribute("cbarvcode", rdRecord01Parameter.getCbarvcode());// 传入参数：采购订单号
			}
			headRow.addAttribute("ddate", DateUtil.getDateFormat(rdRecord01Parameter.getDdate()));// 传入参数：入库日期
			if(rdRecord01Parameter.getDarvdate() != null){
				headRow.addAttribute("darvdate", DateUtil.getDateFormat(rdRecord01Parameter.getDarvdate()));// 传入参数：入库日期
			}
			if(rdRecord01Parameter.getDnverifytime() != null){
				headRow.addAttribute("dnverifytime", DateUtil.getDateTimeFormat(rdRecord01Parameter.getDnverifytime()));// 传入参数：入库日期
			}
			if(rdRecord01Parameter.getCmemo() != null){
				headRow.addAttribute("cmemo", rdRecord01Parameter.getCmemo());// 传入参数：到货单号
			}
			if(rdRecord01Parameter.getCarvcode() != null){
				headRow.addAttribute("carvcode", rdRecord01Parameter.getCarvcode());// 传入参数：到货单号
			}
			headRow.addAttribute("cmaker", rdRecord01Parameter.getCmaker());// 传入参数：制单人
			headRow.addAttribute("chandler", rdRecord01Parameter.getChandler());// 传入参数：制单人
			if(rdRecord01Parameter.getCpersoncode() != null){
				headRow.addAttribute("cpersoncode", rdRecord01Parameter.getCpersoncode());// 传入参数：采购订单号
			}
			if(rdRecord01Parameter.getDveridate() != null){
				headRow.addAttribute("dveridate", DateUtil.getDateFormat(rdRecord01Parameter.getDveridate()));// 传入参数：入库日期
			}
			headRow.addAttribute("iexchrate", rdRecord01Parameter.getIexchrate().toString());// 传入参数：汇率
			if(rdRecord01Parameter.getDnmaketime() != null){
				headRow.addAttribute("dnmaketime", DateUtil.getDateFormat(rdRecord01Parameter.getDnmaketime()));// 传入参数：制单日期
			}
			headRow.addAttribute("crdcode", rdRecord01Parameter.getCrdcode());// 传入参数：制单日期
			headRow.addAttribute("cexch_name", rdRecord01Parameter.getCexch_name());// 传入参数：制单日期
			headRow.addAttribute("vt_id", "89");// 传入参数：单据模板号
			Element body = voucher.addElement("body");
			int rowno = 1;
			for (RdRecords01Parameter rdRecords01Parameter : rdRecord01Parameter.getRdRecords01ParameterList()) {
				Element bodyRow = body.addElement("z:row");
				bodyRow.addAttribute("cinvcode", rdRecords01Parameter.getCinvcode());// 传入参数：存货编码
				bodyRow.addAttribute("iquantity", rdRecords01Parameter.getIquantity().toString());// 传入参数：数量
				if(rdRecords01Parameter.getCbarvcode() != null){
					headRow.addAttribute("cbarvcode", rdRecords01Parameter.getCbarvcode());// 传入参数：制单日期
				}
				if(rdRecords01Parameter.getIarrsid() != null){
					headRow.addAttribute("iarrsid", rdRecords01Parameter.getIarrsid());// 传入参数：制单日期
				}
				if(rdRecords01Parameter.getCbarvdate() != null){
					headRow.addAttribute("cbarvdate", DateUtil.getDateFormat(rdRecords01Parameter.getCbarvdate()));// 传入参数：制单日期
				}
				if(rdRecords01Parameter.getCpoid() != null){
					headRow.addAttribute("cpoid", rdRecords01Parameter.getCpoid());// 传入参数：制单日期
				}
				if(rdRecords01Parameter.getIposid() != null){
					headRow.addAttribute("iposid", rdRecords01Parameter.getIposid());// 传入参数：制单日期
				}
				if(rdRecords01Parameter.getIunitcost() != null) {
					bodyRow.addAttribute("iunitcost", rdRecords01Parameter.getIunitcost().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIprice() != null) {
					bodyRow.addAttribute("iprice", rdRecords01Parameter.getIprice().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIaprice() != null) {
					bodyRow.addAttribute("iaprice", rdRecords01Parameter.getIaprice().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIoritaxcost() != null) {
					bodyRow.addAttribute("ioritaxcost", rdRecords01Parameter.getIoritaxcost().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIoricost() != null) {
					bodyRow.addAttribute("ioricost", rdRecords01Parameter.getIoricost().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIorimoney() != null) {
					bodyRow.addAttribute("iorimoney", rdRecords01Parameter.getIorimoney().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIoritaxprice() != null) {
					bodyRow.addAttribute("ioritaxprice", rdRecords01Parameter.getIoritaxprice().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIorisum() != null) {
					bodyRow.addAttribute("iorisum", rdRecords01Parameter.getIorisum().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getItaxrate() != null) {
					bodyRow.addAttribute("itaxrate", rdRecords01Parameter.getItaxrate().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getItaxprice() != null) {
					bodyRow.addAttribute("itaxprice", rdRecords01Parameter.getItaxprice().toString());// 传入参数：采购订单号
				}
				if(rdRecords01Parameter.getIsum() != null) {
					bodyRow.addAttribute("isum", rdRecords01Parameter.getIsum().toString());// 传入参数：采购订单号
				}
				if(StringUtils.isNotEmpty(rdRecords01Parameter.getCbatch())) {
					bodyRow.addAttribute("cbatch", rdRecords01Parameter.getCbatch());// 传入参数：批号
				}
				bodyRow.addAttribute("irowno", String.valueOf(rowno));// 行号
				rowno++;
			}
			
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 设置编码格式
			format.setEncoding("UTF-8");
			
			return document.asXML();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
